You are given a histogram represented by an array arr, where each element represents the height of a bar, and all bars have a uniform width of 1 unit. Your task is to determine the largest rectangular area that can be formed using one or more consecutive bars in the histogram.
Example 1
Input: arr[] = [6, 2, 5, 4, 5, 1, 6]
Output: 12
Explanation: The largest rectangle has a height of 4 and width of 3, giving an area of 4 × 3 = 12. This is formed by choosing the 3rd, 4th, and 5th bars.
Example 2
Input: arr[] = [2, 4, 6, 3]
Output: 9
Explanation: The largest rectangle has a height of 3 and width of 3, giving an area of 3 × 3 = 9. This is formed by choosing the 2nd, 3rd, and 4th bars.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
6 2 5 4 5 1 6
12
2 4
4